package cn.ekuma.data.ui.android.editor;

import java.util.Properties;

import com.openbravo.data.basic.BasicException;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public abstract class AbstractDAOJEditor<T> extends FrameLayout implements AndroidEditorRecord<T>{

	protected T obj;
    protected int state=STATE_EOF;
    protected DirtyManager dirty;

    public static final int REQUEST_CODE=100;

	public AbstractDAOJEditor(DirtyManager dirty,Context context, AttributeSet attrs) {
		super(context, attrs);
		this.dirty = dirty;
		init(context);
	}

    public AbstractDAOJEditor(DirtyManager dirty,Context context) {
        super(context);
        this.dirty = dirty;
        init(context);
    }

	public abstract void init(Context context);
	
	public void writeValueInsert(Properties prop) {
        state=STATE_Insert;
        obj=createNew(prop);
        updateUI(true,state);
    }

    public void writeValueDelete(T value) {
       state=STATE_Delete;
       obj=value;
       updateUI(false,state);
    }

    public void writeValueEOF() {
        state=STATE_EOF;
        obj=createNew(new Properties());
        updateUI(false,state);
    }

    public void writeValueEdit(T value) {
       state=STATE_Edit;
       obj=value;
       updateUI(true,state);
    }

    public T createValue() throws BasicException {
        if(dirty.isDirty())
            rebuild(obj);
        String retMsg=validator(obj);
        if(retMsg!=null)
            throw new BasicException(retMsg);
        return obj;
    }

    public abstract T createNew(Properties prop);

    public abstract void rebuild(T obj)throws BasicException ;

    public abstract void updateUI(boolean editAble,int state);

    public String validator(T obj){
        return null;
    }

    public DirtyManager getDirty() {
        return dirty;
    }

    public void setDirty(DirtyManager dirty) {
        this.dirty = dirty;
    }

    public int getState() {
        return state;
    }

    public abstract Class getEditClass();

    public void activate() throws BasicException{
    }

	public String getLogerString() {
		return null;
	}
	
	public boolean isLogAble() {
		return false;
	}

	public void hostSelected(T item) {
	}

    public void onActivityResult(int requestCode, int resultCode, Intent data){

    }

}
